package io.github.uicdb.et.entity;

import io.github.uicdb.et.ElementalTheory;
import io.github.uicdb.et.ModAttachments;
import io.github.uicdb.et.ModEntityType;
import lombok.Getter;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class ShadowDefenseOrbEntity extends Entity {

    private int age;

    @Getter
    public float value;


    public ShadowDefenseOrbEntity(Level level, double x, double y, double z, float value) {
        this(ModEntityType.DEFENSE_ORB_SHADOW.get(), level);
        this.setPos(x, y, z);
        this.value = value;
    }

    public ShadowDefenseOrbEntity(EntityType<? extends ShadowDefenseOrbEntity> entityType, Level level) {
        super(entityType, level);
    }

    protected @NotNull MovementEmission getMovementEmission() {
        return MovementEmission.NONE;
    }

    protected void defineSynchedData(SynchedEntityData.@NotNull Builder builder) {
    }

    protected double getDefaultGravity() {
        return 1;
    }

    public void tick() {
        super.tick();
        this.xo = this.getX();
        this.yo = this.getY();
        this.zo = this.getZ();
        ++this.age;
        if (this.age >= 6000) {
            this.discard();
        }

    }

    public @NotNull BlockPos getBlockPosBelowThatAffectsMyMovement() {
        return this.getOnPos(0.999999F);
    }


    public boolean hurt(@NotNull DamageSource source, float amount) {
        return false;
    }

    public void addAdditionalSaveData(CompoundTag compound) {
        compound.putShort("Age", (short) this.age);
        compound.putFloat("Value", (float) this.value);
    }

    public void readAdditionalSaveData(CompoundTag compound) {

        this.age = compound.getShort("Age");
        this.value = compound.getFloat("Value");
    }

    public static final ResourceLocation modifierIdShadow = ResourceLocation.fromNamespaceAndPath(ElementalTheory.MODID, "defense_orb_shadow");

    public void playerTouch(@NotNull Player entity) {
        apply(entity);
    }
    @Override
    public void baseTick() {
        super.baseTick();
        var aabb = this.getBoundingBox().inflate(1.0, 0.5, 1.0);
        List<Entity> list = this.level().getEntities(this, aabb);
        for (Entity entity : list) {
            if(entity instanceof LivingEntity livingEntity) {
                apply(livingEntity);
                return;
            }
        }
    }
    private void apply(@NotNull LivingEntity entity) {
        if (entity.getAttributes().hasAttribute(Attributes.ARMOR)) {
            var a = entity.getAttributes().getInstance(Attributes.ARMOR);
            if (a != null) {
                double r;
                if (a.hasModifier(modifierIdShadow)) {
                    double amount = a.getModifier(modifierIdShadow).amount();
                    r = Math.min(amount, value);
                } else {
                    r = value;
                }
                a.addOrReplacePermanentModifier(new AttributeModifier(modifierIdShadow, r, AttributeModifier.Operation.ADD_MULTIPLIED_BASE));
                entity.setData(ModAttachments.DEFENSE_ORB_SHADOW, -(float) r * 10f);
            }
        }
        this.discard();
    }


    public boolean isAttackable() {
        return false;
    }


    public @NotNull SoundSource getSoundSource() {
        return SoundSource.AMBIENT;
    }
}
